package com.ruoyi.legal.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysFile;
import com.ruoyi.system.service.ISysFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.legal.mapper.MeetingMapper;
import com.ruoyi.legal.domain.Meeting;
import com.ruoyi.legal.service.IMeetingService;
import org.springframework.util.FileCopyUtils;


/**
 * 会议纪要Service业务层处理
 * 
 * @author xwj
 * @date 2025-05-27
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements IMeetingService {
    private static final Logger log = LoggerFactory.getLogger(MeetingServiceImpl.class);

    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    private ISysFileService sysFileService;

    /**
     * 查询会议纪要
     * 
     * @param id 会议纪要主键
     * @return 会议纪要
     */
    @Override
    public Meeting selectMeetingById(Long id) {
        return meetingMapper.selectMeetingById(id);
    }

    /**
     * 查询会议纪要列表
     * 
     * @param meeting 会议纪要
     * @return 会议纪要
     */
    @Override
    public List<Meeting> selectMeetingList(Meeting meeting) {
        return meetingMapper.selectMeetingList(meeting);
    }

    /**
     * 新增会议纪要
     * 
     * @param meeting 会议纪要
     * @return 结果
     */
    @Override
    public Meeting insertMeeting(Meeting meeting) {

      //  meetingMapper.insertMeeting(meeting);
    save(meeting);
    return meeting;
    }

    /**
     * 修改会议纪要
     * 
     * @param meeting 会议纪要
     * @return 结果
     */
    @Override
    public Meeting updateMeeting(Meeting meeting) {

      //  meetingMapper.updateMeeting(meeting);
        updateById(meeting);
        return meeting;
    }

    /**
     * 批量删除会议纪要
     * 
     * @param ids 需要删除的会议纪要主键
     * @return 结果
     */
    @Override
    public int deleteMeetingByIds(Long[] ids) {
        return meetingMapper.deleteMeetingByIds(ids);
    }

    /**
    * 条件删除
    * @param meeting 会议纪要
    * @return
    */
    @Override
    public int delete(Meeting meeting) {
        QueryWrapper<Meeting> qw = new QueryWrapper<>();
        QueryTool.condition(qw, meeting);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param meeting 会议纪要
     * @return
     */
    @Override
    public Pages<Meeting> queryPage(Meeting meeting) {
        QueryWrapper<Meeting> qw = new QueryWrapper<>();
        QueryTool.condition(qw, meeting);
        IPage<Meeting> ipage = new Query<Meeting>().getPage(meeting);

        IPage<Meeting> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }



    /**
     * 删除会议纪要信息
     * 
     * @param id 会议纪要主键
     * @return 结果
     */
    @Override
    public int deleteMeetingById(Long id) {
        return meetingMapper.deleteMeetingById(id);
    }

    @Override
    public String generTempFile(List<Meeting> meetinglist) {
        String base = RuoYiConfig.getUploadPath();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = new Date();
        String currentTime = File.separatorChar + dateFormat.format(date1) + File.separatorChar + date1.getTime();

        String downBase = base + File.separatorChar + "download" + currentTime;

        log.info("downBase：{} ", downBase);
        String xtlx = "meeting";
        try {
            // 循环会议，生成文件
            for(Meeting meeting : meetinglist) {
                Long rid = meeting.getId();
                String name = meeting.getName();

                String dir = downBase + File.separatorChar + name + File.separatorChar;

                FileUtil.mkdir(dir);
                List<SysFile> sysFiles = sysFileService.selectSysFileListByRelateId(rid, xtlx);

                for(SysFile sysFile: sysFiles) {
                    String filePath = base + sysFile.getUrl();
                    String oriFileName = sysFile.getOriFileName();

                    File file = new File(filePath);
                    if(file.exists()) {
                        FileCopyUtils.copy(file, new File(dir, oriFileName));
                    }
                }
            }

            ZipUtil.zip(downBase);

            FileUtil.del(downBase);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return downBase + ".zip";
    }


}
